package talk;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client {
	public static void main(String[] args) throws IOException {
		JFrame frame=new JFrame("客户端1：");
		JTextArea text = new JTextArea(500,300);
		text.setEditable(false);
		//JScrollPane scr = new JScrollPane(text,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		frame.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
		Box box=new Box(BoxLayout.X_AXIS);
		
		JButton sent=new JButton("发送");
		sent.setMnemonic(KeyEvent.VK_ENTER);
		JTextField text1 = new JTextField(20) ;
		
		box.add(text1);
		box.add(sent);
		frame.setLayout(new BorderLayout());
		
		frame.add(text, BorderLayout.CENTER);
		frame.add(box, BorderLayout.SOUTH);
		sent.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(e.getSource()==sent){
					String t=text1.getText();
					text.append("我："+t+"\n");
					try {
						Client1.serversend(t);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					text1.setText("");
				}
			}
		
		});
		frame.setSize(500, 400);
	    frame.setVisible(true);
	    try {
			
			ServerSocket server1 = new ServerSocket(8080);
			while( true ){
				Socket client1 = server1.accept();
				InetAddress ip1 = client1.getInetAddress();
				InputStream in1 = client1.getInputStream();
				DataInputStream dis1 = new DataInputStream(in1);
				String msg = dis1.readUTF();
				text.append("客户端2："+msg+"\n");
				OutputStream out1 = client1.getOutputStream();
				DataOutputStream dos1 = new DataOutputStream(out1);
				dos1.close();
				out1.close();
				dis1.close();
				in1.close();
				client1.close();
				dos1.flush();
				out1.flush();				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}